#pragma once

#include <QThread>
#include <vector>

#include "TableCommon.h"

namespace qb {
namespace ht {
using namespace std;

typedef void (*pTaskFunc)(void*);

struct TaskItem {
  pTaskFunc proc;
  void* param;
  TaskItem(pTaskFunc func = 0, void* p = 0) : proc(func), param(p) {}
};
class CThreadProcess : public QThread {
  Q_OBJECT

 public:
  CThreadProcess();

  void start();
  void run();
  void stop();

  void AddTask(pTaskFunc task, void* param);
  bool GetTask(TaskItem& task);
  int GetTaskCount();
  void CallTask(pTaskFunc task, void* param);
  bool isidle() { return m_is_idle; }
  void SetWaitTime(unsigned long waitTime);

 public slots:
  void onTimer();

 private:
  std::vector<TaskItem> m_tasks;
  FastLock m_lock;
  unsigned long m_msWait;
  bool m_is_idle;
};

class CTaskThread {
  CThreadProcess* m_process = nullptr;

 public:
  CTaskThread();
  void AddTask(pTaskFunc task, void* param);
  bool isidle();
  void SetWaitTime(unsigned long waitTime);
  void Close();
};
enum kThreadMode { kThreadModeFast, kThreadModeSlow };
class ThreadPool {
  std::vector<CTaskThread*> m_threads;
  int m_next_thread;  // 上次那个线程
  static ThreadPool* instance_;

 public:
  ThreadPool();
  ~ThreadPool();
  static ThreadPool* GetInstance();
  bool PostTask(pTaskFunc task, void* param);
  size_t GetThreadCount() { return m_threads.size(); }
  void ChangeMode(kThreadMode mode);
};
}  // namespace ht
}  // namespace qb
